rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer",
             "fastDummies","ICCbin","ihs","readstata13","xtable",
             "arm","ICCbin")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages, new.packages)

setwd("PUT YOUR DIRECTORY HERE")

## insheet data
data <- read.csv("./Datasets/panel_bare_bones.csv")

## clean data
data$X <- NULL
data$locality_year <- NULL
data$subbotnik_inkind <- NULL

# generate dummies for year and locality
data$year <- as.character(data$year)
data$locality <- as.character(data$locality)
data <- fastDummies::dummy_cols(data)
data$year <- as.numeric(data$year)

######################################
## Figure out effective sample size ##
######################################

effective_sample_fun <- function(data, FE, treatment, outcome){
  cluster_stats <- data %>%
    dplyr::group_by((!!as.symbol(FE))) %>%
    dplyr::filter(!is.na((!!as.symbol(outcome)))) %>%
    dplyr::summarise(var_of_treatment = var((!!as.symbol(treatment)), na.rm = TRUE),
                     var_of_outcome = var((!!as.symbol(outcome)), na.rm = TRUE),
                     n(),
                     effective_use = ifelse(var_of_treatment != 0 & var_of_outcome != 0, 1, 0))
  print(paste("n", FE, "with variation", sep = " "))
  print(table(cluster_stats$effective_use))
  print("n observations to be used")
  print(sum(cluster_stats$`n()`[cluster_stats$effective_use == 1], na.rm = TRUE))
  effective_sample <- cluster_stats[which(cluster_stats$effective_use == 1),][,1]
  return(effective_sample)
}

##### EFFECTIVE NUMBER OF OBS FOR YEAR FIXED EFFECTS

## effective sample  for year FE with strikes
effective_sample <- effective_sample_fun(data = data, FE = "year", treatment = "commanders", outcome = "strikes")

## effective sample  for year FE with subbotnik
effective_sample <- effective_sample_fun(data = data, FE = "year", treatment = "commanders", outcome = "subbotnik_inkind_")


##### EFFECTIVE NUMBER OF OBS FOR LOCALITY  FIXED EFFECTS

## effective sample  for locality FE with strikes
effective_sample <- effective_sample_fun(data = data, FE = "locality", treatment = "commanders", outcome = "strikes")

## effective sample  for locality FE with subbotnik
effective_sample <- effective_sample_fun(data = data, FE = "locality", treatment = "commanders", outcome = "subbotnik_inkind_")


